package com.hxzy.controller.system;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.common.core.controller.BaseController;
import com.hxzy.common.core.domain.R;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.base.BaseException;
import com.hxzy.system.domain.SysNotice;
import com.hxzy.system.domain.dto.SysNoticeSearchDTO;
import com.hxzy.system.service.ISysNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

@Api(tags = "通知API")
@RestController
@RequestMapping(value = "/api")
public class SysNoticeController extends BaseController {

    @Autowired
    private ISysNoticeService iSysNoticeService;

    @GetMapping(value = "/system/notice/data")
    @ApiOperation(value = "公告查询接口")
    public R search(SysNoticeSearchDTO sysNoticeSearchDTO){
        IPage<SysNotice> search = this.iSysNoticeService.search(sysNoticeSearchDTO);
        R r = super.pageVO(search);
        if (Objects.isNull(r)){
            throw new BaseException("查询数据异常");
        }
        return r;
    }

    @GetMapping(value = "/system/notice/{noticeId}")
    @ApiImplicitParam(value = "noticeId" , name = "公告ID" , example = "1")
    @ApiOperation(value = "根据ID查询公告数据")
    public R searchById(@PathVariable(value = "noticeId") Long noticeId){
        SysNotice sysNotice = this.iSysNoticeService.searchById(noticeId);
        if(Objects.isNull(sysNotice)){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(sysNotice);
    }

    @PostMapping(value = "/system/notice/add")
    @ApiModelProperty(value = "公告新增")
    public R add(@RequestBody @Valid SysNotice sysNotice){
        boolean save = this.iSysNoticeService.save(sysNotice);
        return save ? R.build(AckCode.SUCCESS) : R.build(AckCode.NOT_UPDATE);
    }

    @PutMapping(value = "/system/notice/update")
    @ApiOperation(value = "公告修改")
    public R update(@RequestBody @Valid  SysNotice sysNotice){
        boolean update = this.iSysNoticeService.updateById(sysNotice);
        return update ? R.build(AckCode.SUCCESS) : R.build(AckCode.NOT_UPDATE);
    }
}
